package org.applpi.core;

import com.sun.enterprise.module.bootstrap.ModuleStartup;
import com.sun.enterprise.module.bootstrap.StartupContext;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.applpi.plugin.Plugin;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.component.Habitat;

/**
 * This class is responsible for bootstrapping the server
 */
@Service
public class Startup implements ModuleStartup
{
    public static Logger LOGGER = Logger.getLogger((Startup.class.getName()));
    StartupContext context;
    
    @Inject
    Habitat habitat;
        
    public void setStartupContext(StartupContext context) {
        this.context = context;
    }
    
    public void start() {
        // TODO - Detect newly installed plugins without restart
        
        System.out.println("==========HERE");

        Collection<Plugin> plugins = habitat.getAllByContract(Plugin.class);

        for (final Plugin plugin : plugins) {
            try {
                System.out.println("======= Running plugin " + plugin.getName());
                plugin.run();
            } catch (Exception e) {
                LOGGER.log(Level.SEVERE, null, e);
            }
        }        
    }

    public void stop() {
    }
}
